<?php

namespace App\Http\Controllers\Api;

use App\Exports\ProductExport;
use App\Http\Controllers\Controller;
use App\Rep\ProductRep;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class Product extends Controller
{
    //产品管理
    protected $rep;

    public function __construct(Request $request)
    {
        $this->rep = new ProductRep();
    }

    /**
     * 获取产品列表
     * product_name
     * color_id
     *
     */
    public function getProductList(Request $request){
        $params = $request->all();
        $export = $request->get('export')??0;
        //p($export);
        if($export == 1){
            try{
                $params['order_name'] = '产品列表';
                $filename = date('ymdhis').'.xlsx';
                $export = new ProductExport($params);
                return Excel::download($export,$filename,\Maatwebsite\Excel\Excel::XLSX,[
                    'Content-Type'=> 'application/octet-stream',
                    'Content-Disposition'=> 'attachment; filename='.$filename,
                    'Content-Transfer-Encoding'=> 'binary',
                ]);
            }catch(\Exception $exception){
                return re(0,[],$exception->getMessage());
            }
        }else{
            if(isset($params['is_dropdown']) && !empty($params['is_dropdown'])){
                $data = $this->rep->getProductDropDownListRep($params);
                //p($data);
            }else{
                $data = $this->rep->getProductListRep($params);
            }

            return re(0,$data,'success');
        }

    }

    /**
     * 添加产品
     * id
     * product_name
     * color_ids      array[]
     */
    public function createProduct(Request $request){
        //$params = $request->getContent();
        $params = $request->all();
        $data = $this->rep->createProductRep($params);
        return re(0,$data['data'],$data['msg']);
    }

    /**删除**/
    public function delProduct(Request $request){
        $params = $request->all();
        $data = $this->rep->delProductRep($params);
        return re(0,$data,'success');
    }

    /**
     * 获取产品的关联型号列表
     */
    public function getModelNumberByProduct(Request $request){
        $product_id = $request->input('product_id');
        $data = $this->rep->getModelNumberByProductRep($product_id);
        return re(0,$data,'success');
    }

    /**
     * 获取产品颜色列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getProductColorList(Request $request){
        $params = $request->all();
        $data = $this->rep->getProductColorListRep($params);
        return re(0,$data,'success');
    }

    /**
     * 添加产品颜色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addProductColor(Request $request){
        //$params = $request->getContent();
        $params = $request->all();
        $data = $this->rep->addProductColorRep($params);
        return re(0);
    }

    /**
     * 删除产品颜色
     * @param Request $request
     */
    public function delProductColor(Request $request){
        $params = $request->all();
        $data = $this->rep->delProductColorRep($params);
        return re(0);
    }

    /**
     * 获取产品工序
     */
    public function getProductJobTypeList(Request $request){
        $params = $request->all();
        $data = $this->rep->getProductJobTypeListByProductId($params);
        return re(0,$data,'success');
    }


    /**
     * 添加产品工种
     */
    public function addProductJobType(Request $request){
        $params = $request->all();
        $data = $this->rep->addProductJobTypeRep($params);
        return re(0);
    }


    /**
     * 设置工序
     */
    public function setProductJobTypeListSort(Request $request){
        $product_id = $request->input('product_id');
        $job_type_id = $request->input('job_type_id');
        $data = $this->rep->setProductJobTypeListSortRep($product_id,$job_type_id);
        return re(0,$data);
    }

    /**
     * 移除产品工种关联
     */
    public function delProductJobType(Request $request){
        $params = $request->all();
        $data = $this->rep->delProductJobTypeRep($params);
        return re(0);
    }

    /**
     * 设置产品工种排序
     * @param Request $request
     * $product_id  产品id
     * $sort_info   排序详情
     */
    public function setProductJobTypeSort(Request $request){
        $params = $request->all();
        $data = $this->rep->setProductJobTypeSortRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }

    /**
     * 获取产品工种排序
     * @param Request $request
     */
    public function getProductJobTypeSort(Request $request){
        $params = $request->all();
        $data = $this->rep->getProductJobTypeSortRep($params);
        return re(0,$data,'success');
    }

    public function getSemiListByProductId(Request $request){
        $product_id = $request->input('product_id');
        $data = $this->rep->getSemiListByProductIdRep($product_id);
        return re(0,$data,'success');
    }

    /**
     * 产品批量导入
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function productImport(Request $request){
        $file_url = $request->input('file_url');
        $data = $this->rep->productImportRep($file_url);
        return re($data['code'],$data['data'],$data['msg']);
    }

}
